<template>
    <view class="content">
        <view class="box">
            <view class="item">
                <view class="grey">发货方</view>
                <input type="text" v-model="name" placeholder="请输入发货方" class="inp">
            </view>
            <view class="item">
                <view class="grey">联系人</view>
                <input type="text" v-model="contacts" placeholder="请输入联系人" class="inp">
            </view>
            <view class="item">
                <view class="grey">联系方式</view>
                <input type="number" v-model="phone" placeholder="请输入联系方式" class="inp">
            </view>
            <view class="item">
            	<view class="grey">设置为默认</view>
            	<u-switch v-model="checked" size="40" active-color="#ff5f2b" @change="change"></u-switch>
            </view>
        </view>
        <view class="address-btn" @click="doSubmit">保存</view>
    </view>
</template>

<script>
	export default {
		data() {
			return {
                name: '',//发货方
				contacts: '',//联系人
                phone: '',//联系方式
                checked: true,
                id: '',
			}
		},
        onLoad(option) {
            if(option.id){
            	uni.setNavigationBarTitle({
            		title:'修改地址'
            	})
                this.id = option.id
            	this.getData()
            }
        },
		methods: {
			change(status) {
				console.log(status);
                this.checked = status;
			},
			
            doSubmit(){
                if(!this.name){
                	this.$toast('请输入发货方')
                	return
                }
				if(!this.contacts){
					this.$toast('请输入联系人')
					return
				}
                if(!this.phone){
                	this.$toast('请输入联系方式')
                	return
                }
                if(!this.$checkPhone(this.phone)){
                	this.$toast('请输入正确的联系方式')
                	return
                }
            	this.$ajax('insert_files_address', {
                    user_token: this.$getSync('userToken'),
					type: 1,//1发货方 2收货方
            		name: this.name,//名称(发货方)
					contacts: this.contacts,//联系人
            		phone: this.phone,//联系方式
                    address: '',//经营位置
                    is_default: this.checked ? 1 : 0, //1是 0否
					id: this.id ? this.id : '', //地址id 编辑传
            	}).then(ret => {
            		if (ret.success == 1000) {
                        this.$toast('添加成功！');
            			uni.navigateBack({
            				delta:1
            			})
            		} else {
            			this.$toast(ret.msg);
            		}
            	});
            },
            
            getData(){
            	this.$ajax('files_address', {
            		user_token: this.$getSync('userToken'),
					type: 1,//1发货方 2收货方
					status: 2,//1自己的 2自己的+平台的
					id: this.id,//地址id
					name: '',//搜索
            	}).then(ret => {
            		if (ret.success == 1000) {
                        let data = ret.detail[0]
                        this.name = data.name
                        this.contacts = data.contacts
						this.phone = data.phone
                        this.checked = data.is_default == 1 ? true : false
            		} else {
            			this.$toast(ret.msg);
            		}
            	});
            },
		}
	}
</script>

<style lang="scss">
    page {
    	background-color: #eef1f6;
    }
    .content {
        padding: 20rpx;
    }
    
    .box {
        padding: 10rpx 25rpx;
        background-color: #fff;
        border-radius: 20rpx;
    }
    .item{
        padding: 30rpx 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .grey {
    	min-width: 160rpx;
    	font-size: 28rpx;
    	color: #656565;
    	text-align: left;
    }
    .inp{
        flex: 1;
    }
    .address-btn {
        font-size: 34rpx;
        color: #fff;
        text-align: center;
        padding: 25rpx;
        background-color: #00457f;
        border-radius: 50rpx;
        margin: 70rpx auto;
    }
</style>
